package com.wo.cloud.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wo.cloud.admin.entity.SysRole;
import com.wo.cloud.admin.service.IRoleService;
import com.wo.cloud.admin.util.SysFeignConstant;
import com.wo.cloud.core.entity.Response;
import com.wo.cloud.log.annotation.Log;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/15 22:43
 * Description: 角色管理
 */

@RestController
@RequestMapping(SysFeignConstant.SYS_ROLE_BASE_URL)
@Api(tags = "角色管理模块")
public class SysRoleController {

    @Autowired
    private IRoleService roleService;

    // 新增角色
    @PostMapping("/save")
    @Log
    public Response create(@RequestBody SysRole sysRole) {
        roleService.saveOrUpdate(sysRole);
        if (StrUtil.isNotBlank(sysRole.getId())) {
            return Response.success();
        }
        return Response.fail();
    }


    // 删除角色
    @DeleteMapping("/{id}")
    @Log
    public Response deleteById(@PathVariable("id") String id) {
        boolean removeById = roleService.removeById(id);
        if (removeById) {
            return Response.success();
        }
        return Response.fail();
    }

    // 修改角色
    @GetMapping("/{id}")
    @Log
    public Response<SysRole> getById(@PathVariable("id") String id) {
        SysRole sysRole = roleService.getById(id);
        if (ObjectUtil.isNotNull(sysRole)) {
            return Response.fail("该数据不存在");
        }
        return Response.data(sysRole);
    }

    // 分页条件查询
    @GetMapping("/page")
    @Log
    public Response<IPage<SysRole>> page(Page page, @RequestParam(required = false) String name) {
        IPage<SysRole> sysRoleIPage = roleService.selectPage(page, name);
        return Response.data(sysRoleIPage);
    }

    // 分页列表
    @GetMapping("/list")
    @Log
    public Response<List<SysRole>> list() {
        List<SysRole> list = roleService.list();
        return Response.data(list);
    }
}
